package com.android.huimeiqupin.fragment.detail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.huimeiqupin.fragment.BaseLazyFragment;
import com.android.huimeiqupin.R;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 商品图文详情
 */
public class Fragment_Detail_GoodsDetail_DetailWeb extends BaseLazyFragment {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.web_nodata)
    TextView nodata;
    private FragmentActivity mcontext;
    private View view;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;

    public Fragment_Detail_GoodsDetail_DetailWeb() {


    }

    public static Fragment_Detail_GoodsDetail_DetailWeb newInstance(String param1) {
        Fragment_Detail_GoodsDetail_DetailWeb fragment = new Fragment_Detail_GoodsDetail_DetailWeb();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        initUI();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_detail_goods_detail_web, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    private void initUI() {
        if (mParam1 == null || mParam1.equals("")) {
            progress.setVisibility(View.GONE);
            nodata.setVisibility(View.VISIBLE);
        } else {
            webView.setWebViewClient(new MyWebViewClient());
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    if (newProgress == 100) {
                        progress.setVisibility(View.GONE);
                    }
                }
            });
            nodata.setVisibility(View.GONE);
            //宽度适应屏幕
            String js = "<style>\n" +
                    "img{\n" +
                    " max-width:100%;\n" +
                    "height:auto\n" +
                    "}\n" +
                    "</style>" + mParam1;

            webView.loadDataWithBaseURL(null, js, "text/html", "utf-8", null);
        }
    }

    class MyWebViewClient extends WebViewClient {
        //拦截网址
    }
}
